import 'dart:convert';
import 'package:dio/dio.dart';
import '../model/conversation_detail.dart';

class Services {
  static const String url = "http://v.juhe.cn/toutiao/index";
  static const String key = "4c52313fc9247e5b4176aed5ddd56ad7";
  static const String type = "keji";

  static Future<List<ConversationDetail>> getDatas() async {
    try {
      final response =
          await Dio().get(url, queryParameters: {"type": type, "key": key});
      if (response.statusCode == 200) {
        List<ConversationDetail> list = parseDatas(response.data);
        return list;
      } else {
        throw Exception('Error');
      }
    } catch (e) {
      throw Exception(e.toString());
    }
  }

  static List<ConversationDetail> parseDatas(String responseBody) {
    final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();
    return parsed
        .map<ConversationDetail>((json) => {ConversationDetail.fromJson(json)})
        .toList();
  }
}
